﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VemTransporte.Models.Basica;
using VemTransporte.Models.Factory;
using VemTransporte.Models.Fachada;
using System.Globalization;

namespace VemTransporte.Views.UsuarioTransporte
{
    public partial class Cadastro : System.Web.UI.Page
    {
        private Usuario novoUsuario = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownListDia.Items.Clear();
                DropDownListDia.Items.Add("Dia");
                DropDownListMes.Items.Clear();
                DropDownListMes.Items.Add("Mês");
                DropDownListAno.Items.Clear();
                DropDownListAno.Items.Add("Ano");

                for (int i = 1; i <= 31; i++)
                {
                    DropDownListDia.Items.Add(string.Format("{0:00}", i));
                }
                CultureInfo culture = new CultureInfo("pt-BR");
                DateTimeFormatInfo dtfi = culture.DateTimeFormat;

                for (int i = 1; i <= 12; i++)
                {
                    string mes = culture.TextInfo.ToTitleCase(dtfi.GetMonthName(i));
                    DropDownListMes.Items.Add(mes);
                }

                for (int i = 1910; i <= 2011; i++)
                {
                    DropDownListAno.Items.Add(Convert.ToString(i));
                }

                DropDownListSexo.Items.Clear();
                DropDownListSexo.Items.Add("Masculino");
                DropDownListSexo.Items.Add("Feminino");


            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void CreateUserButton_Click(object sender, EventArgs e)
        {
            try
            {
                novoUsuario = PessoaFactory.Instance.CreateUsuario();

                novoUsuario.Nome = UserName.Text;
                novoUsuario.Email = Email.Text;
                novoUsuario.Senha = Password.Text;

                novoUsuario.Nascimento = DateTime.Parse(
                    DropDownListDia.Items[DropDownListDia.SelectedIndex].Text + "/" +
                    DropDownListMes.SelectedIndex + "/" +
                    DropDownListAno.Items[DropDownListAno.SelectedIndex].Text); ;

                switch (DropDownListSexo.SelectedIndex)
                {
                    case 1: 
                        novoUsuario.Sexo = "M";
                        break;

                    case 2:
                        novoUsuario.Sexo = "F";
                        break;
                }                                           

                Fachada.Instance.Salvar(novoUsuario);

                if (novoUsuario.Id > 0)
                {
                    //Redirect para o mapa implementar aqui
                    Response.Redirect("MenuUsuario.aspx", false);
                }
                else
                {
                    ErrorMessage.Text = "Ocorreu um erro ao tentar criar seu cadastro! Tente novamente!";
                }

            }
            catch (Exception ex)
            {
                ErrorMessage.Text = ex.Message;
            }
           
        }

        protected void ButtonBuscaCEP_Click(object sender, EventArgs e)
        {
        }

    }
}